﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ConvexPolygon
{
    public partial class Polygon : Form
    {
        private List<PointF> polygonPoints = new List<PointF>();
        private Graphics surface;
        private Pen p = new Pen(Color.FromArgb(192, 192, 255), 1.0f);
        private SolidBrush b = new SolidBrush(Color.FromArgb(192, 192, 255));

        public Polygon()
        {
            InitializeComponent();
            surface = pictureBox1.CreateGraphics();
        }


        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            toolStripMousePosition.Text = e.Location.ToString();
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            PointF[] closedPolygon;
            CheckConvexPolygon checker;

            Console.WriteLine("clicK: " + e.Location.ToString());
            if (polygonPoints.Count == 0 || !e.Location.Equals(polygonPoints.Last()))
            {
                polygonPoints.Add(e.Location);

                if (polygonPoints.Count > 1)
                {
                    surface.Clear(pictureBox1.BackColor);
                    //surface.DrawPolygon(p, polygonPoints.ToArray());
                    surface.FillPolygon(b, polygonPoints.ToArray());
                }
                
                closedPolygon = polygonPoints.ToArray();
                Array.Resize(ref closedPolygon, polygonPoints.Count + 1);
                closedPolygon[polygonPoints.Count] = polygonPoints[0];
                checker = new CheckConvexPolygon(closedPolygon);

                if (closedPolygon.Length > 3)
                {
                    lblDaugiakmapis.Text = "Taip";
                    if (checker.ConvexPolygon)
                        toolStripResult.Text = "Iškilusis daugiakampis";
                    else
                        toolStripResult.Text = "Neiškilusis daugiakampis";
                    lblJordan.Text = checker.JordanCurve ? "Taip" : "Ne";
                    lblAngles.Text = checker.ConvexAngles ? "Taip" : "Ne";
                }

            }
            

            
        }

        private void lbl_TextChanged(object sender, EventArgs e)
        {
            Label lbl;
            if (sender is System.Windows.Forms.Label)
            {

                lbl = (Label)sender;
                if (lbl.Text == "Ne")
                {
                    lbl.ForeColor = Color.Red;
                }
                if (lbl.Text == "Taip")
                {
                    lbl.ForeColor = Color.Green;
                }
            }
        }

        private void tollStripInfo_Click(object sender, EventArgs e)
        {
            
        }

        private void apieToolStripMenuItem_Click(object sender, EventArgs e)
        {
            fmrAbout inf = new fmrAbout();
            inf.ShowDialog();
        }

        private void pagalbaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmHelp hlp = new frmHelp();
            hlp.ShowDialog();
        }

        private void btnIstrinti_Click(object sender, EventArgs e)
        {
            polygonPoints.Clear();
            surface.Clear(pictureBox1.BackColor);
            toolStripResult.Text = "Nedaugiakampis";
            lblDaugiakmapis.Text = "Ne";
            lblJordan.Text = "Ne";
            lblAngles.Text = "Ne";
        }


    }
}
